package com.zzh.tree;

public class No110_isBalanced {
    public static void main(String[] args) {
        No110Solution solution = new No110Solution();
        boolean balanced = solution.isBalanced(TreeNode.initTree());
        System.out.println(balanced);
    }
}
class No110Solution{
    public int height(TreeNode root){
        /*
        * 判断当前结点的高度
        * 左右子树的最大高度+1
        * */
        if (root==null){
            return -1;
        }
        return Math.max(height(root.left),height(root.right))+1;
    }
    public boolean isBalanced(TreeNode root) {
        /*
        * 平衡树：当前结点的左子树的高度和右子树的高度相差<=1
        * */
        if (root==null){
            return true;
        }
        return Math.abs(height(root.right)-height(root.left))<=1
                &&isBalanced(root.left)
                &&isBalanced(root.right);
    }
}
